package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.dto.NoticeDTO;
import com.example.entity.Notices;
import com.example.mapper.NoticesMapper;
import com.example.service.NoticesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 公告管理表 服务实现类
 * </p>
 *
 * @author wuyunbin
 * @since 2025-04-03
 */
@Service
public class NoticesServiceImpl extends ServiceImpl<NoticesMapper, Notices> implements NoticesService {

    @Override
    public IPage<Notices> getPage(NoticeDTO noticeDTO) {
        IPage<Notices> pageInfo = new Page<>(noticeDTO.getCurrent(), noticeDTO.getSize());
        LambdaQueryWrapper<Notices> wrapper = new LambdaQueryWrapper<>();
        
        // 根据标题或内容模糊查询
        if (StringUtils.hasText(noticeDTO.getContent())) {
            wrapper.like(Notices::getTitle, noticeDTO.getContent())
                  .or()
                  .like(Notices::getContent, noticeDTO.getContent());
        }
        
        // 日期范围
        if (noticeDTO.getCreatedAt() != null && noticeDTO.getUpdatedAt() != null) {
            wrapper.between(Notices::getCreatedAt, noticeDTO.getCreatedAt(), noticeDTO.getUpdatedAt());
        }
        
        // 按展示状态过滤
        if (noticeDTO.getIsDisplayed() != null) {
            wrapper.eq(Notices::getIsDisplayed, noticeDTO.getIsDisplayed());
        }
        
        // 按创建时间排序，最新的在前面
        wrapper.orderByDesc(Notices::getCreatedAt);
        
        return this.page(pageInfo, wrapper);
    }

    @Override
    public List<Notices> listDisplayedNotices() {
        LambdaQueryWrapper<Notices> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Notices::getIsDisplayed, true);
        wrapper.orderByDesc(Notices::getCreatedAt);
        return this.list(wrapper);
    }
}
